﻿@namespace LaserMark.Components.Components
@using LaserMark.DataAccess.Service

<div class="mark-args">
    <ValidateForm Model="@Model" @ref="ValidateForm">
        <GroupBox Title="打码参数">
            <div class="row g-3 form-inline">
                <div class="col-6">
                    <Switch @bind-Value="Model.IsAutoMark" ShowLabel="true" DisplayText="启用打码"></Switch>
                </div>
                <div class="col-6">
                    <BootstrapInputNumber @bind-Value="Model.MarkAngleOffset" ShowLabel="true" DisplayText="打码角度偏移(°)"></BootstrapInputNumber>
                </div>
                <div class="col-6">
                    <Switch @bind-Value="Model.IsMirrorX" ShowLabel="true" DisplayText="X镜像"></Switch>
                </div>
                <div class="col-6">
                    <Switch @bind-Value="Model.IsMirrorY" ShowLabel="true" DisplayText="Y镜像"></Switch>
                </div>
                <div class="col-6">
                    <Select Items="@SelectInfos" @bind-Value="Model.TemplateMatchProperty" ShowLabel="true" DisplayText="模板匹配字段(OPT)"></Select>
                </div>
                <div class="col-6">
                    <BootstrapInput @bind-Value="Model.WidthAndHeightMatch" ShowLabel="true" DisplayText="Info宽高对应字段(OPT)"></BootstrapInput>
                </div>
            </div>
        </GroupBox>

        <GroupBox Title="打码偏移" class="mt-3">
            <div class="row g-3 form-inline">
                <div class="col-6">
                    <BootstrapInputNumber @bind-Value="Model.OffsetX1" ShowLabel="true" DisplayText="配置1X偏移"></BootstrapInputNumber>
                    <label class="ms-2 form-label">mm</label>
                </div>
                <div class="col-6">
                    <BootstrapInputNumber @bind-Value="Model.OffsetY1" ShowLabel="true" DisplayText="配置1Y偏移"></BootstrapInputNumber>
                    <label class="ms-2 form-label">mm</label>
                </div>
                <div class="col-6">
                    <BootstrapInputNumber @bind-Value="Model.OffsetX2" ShowLabel="true" DisplayText="配置2X偏移"></BootstrapInputNumber>
                    <label class="ms-2 form-label">mm</label>
                </div>
                <div class="col-6">
                    <BootstrapInputNumber @bind-Value="Model.OffsetY2" ShowLabel="true" DisplayText="配置2Y偏移"></BootstrapInputNumber>
                    <label class="ms-2 form-label">mm</label>
                </div>
                <div class="col-6">
                    <BootstrapInputNumber @bind-Value="Model.OffsetX3" ShowLabel="true" DisplayText="配置3X偏移"></BootstrapInputNumber>
                    <label class="ms-2 form-label">mm</label>
                </div>
                <div class="col-6">
                    <BootstrapInputNumber @bind-Value="Model.OffsetY3" ShowLabel="true" DisplayText="配置3Y偏移"></BootstrapInputNumber>
                    <label class="ms-2 form-label">mm</label>
                </div>
                <div class="col-6">
                    <BootstrapInputNumber @bind-Value="Model.OffsetX4" ShowLabel="true" DisplayText="配置4X偏移"></BootstrapInputNumber>
                    <label class="ms-2 form-label">mm</label>
                </div>
                <div class="col-6">
                    <BootstrapInputNumber @bind-Value="Model.OffsetY4" ShowLabel="true" DisplayText="配置4Y偏移"></BootstrapInputNumber>
                    <label class="ms-2 form-label">mm</label>
                </div>
                <div class="col-6">
                    <BootstrapInputNumber @bind-Value="Model.OffsetX5" ShowLabel="true" DisplayText="配置5X偏移"></BootstrapInputNumber>
                    <label class="ms-2 form-label">mm</label>
                </div>
                <div class="col-6">
                    <BootstrapInputNumber @bind-Value="Model.OffsetY5" ShowLabel="true" DisplayText="配置5Y偏移"></BootstrapInputNumber>
                    <label class="ms-2 form-label">mm</label>
                </div>
            </div>
        </GroupBox>
    </ValidateForm>
</div>

<DialogFooter OnSave="@OnSave"></DialogFooter>

@code {

    [NotNull]
    private ValidateForm? ValidateForm { get; set; }

    [Inject]
    [NotNull]
    private IMarkArgsService? MarkArgsService { get; set; }

    [Inject]
    [NotNull]
    private ISecretService? SecretService { get; set; }

    [Inject]
    [NotNull]
    private ToastService? ToastService { get; set; }

    private MarkArgs Model { get; set; } = new();

    [NotNull]
    private List<string>? Infos { get; set; }

    [NotNull]
    private List<SelectedItem>? SelectInfos { get; set; }

    protected override async Task OnInitializedAsync()
    {
        await base.OnInitializedAsync();
        Model = await MarkArgsService.GetMarks();

        Infos = new List<string>()
        {
            "SecondGlassReference",
            "RackNo",
            "SheetWidth",
            "SheetHeight",
            "Customer",
            "PosNo",
            "OrderNo",
            "RackCode",
            "PieceStandardPz",
            "UMPz",
            "IDPz",
            "RealDimXPz",
            "RealDimYPz",
            "GrindingPz",
            "GrindingPzX1",
            "GrindingPzY1",
            "GrindingPzX2",
            "GrindingPzY2",
            "BendingPz",
            "QuantityPz",
            "PriorityPz",
            "LabelsPz",
            "LabelsPzCounter",
            "RotationAllowed",
            "PreferencePz",
            "DescMatCompPz",
            "DeliveryDatePz",
            "PzNOTE",
            "PzNOTE1",
            "PzNOTE2",
            "PzNOTE3",
            "PzNOTE4",
            "PzNOTE5",
            "PzNOTE6",
            "PzNOTE7",
            "PzNOTE8",
            "PzNOTE9",
            "PzNOTE10",
            "PzNOTE11",
            "PzNOTE12",
            "PzNOTE13",
            "PzNOTE14",
            "PzNOTE15",
            "PzNOTE16",
            "PzNOTE17",
            "PzNOTE18",
            "PzNOTE19",
            "PzNOTE20",
            "EXTRADATA1",
            "EXTRADATA2",
            "EXTRADATA3",
            "EXTRADATA4",
            "EXTRADATA5",
            "EXTRADATA6",
            "EXTRADATA7",
            "EXTRADATA8",
            "EXTRADATA9",
            "EXTRADATA10"
        };

        SelectInfos = Infos.Select(s => new SelectedItem(s, s)).ToList();
    }

    private Task OnClick()
    {
        OpenFolderDialog openFolderDialog = new OpenFolderDialog();
        openFolderDialog.DefaultDirectory = AppContext.BaseDirectory;
        var ret = openFolderDialog.ShowDialog();
        if (ret.HasValue && ret.Value)
        {
            Model.MarkTemplatePath = openFolderDialog.FolderName;
        }

        return Task.CompletedTask;
    }

    private async Task<bool> OnSave(string password)
    {
        var ret = ValidateForm.Validate();
        var res = await SecretService.GetSecret("Customer", password);
        if (res)
        {
            await MarkArgsService.UpdateMarks(Model);
            return true;
        }
        else
        {
            return false;
        }
    }
}
